import { Suggestion, SuggestionResult } from '@/types/data'
import { SearchAction } from '@/types/store'

//类型
type SearchState = {
	suggestion: Suggestion['options']
	suggestionResult: SuggestionResult
}

//初始值
const initialState: SearchState = {
	suggestion: [],
	suggestionResult: {
		page: 1,
		per_page: 10,
		total_count: 0,
		results: [],
	},
}

export const search = (
	state = initialState,
	action: SearchAction
): SearchState => {
	switch (action.type) {
		case 'search/suggestion':
			return {
				...state,
				suggestion: action.payload,
			}

		case 'search/clearSuggestion':
			return {
				...state,
				suggestion: [],
			}

		//注意：此处，并没有考虑 搜索结果分页，所以，直接使用 action.payload 来覆盖了数据
		//也就是说，如果要考虑分析，就需要追加 results 数据
		case 'search/getSuggestionResult':
			return {
				...state,
				suggestionResult: action.payload,
			}

		default:
			return state
	}
}
